import random


def generate_random_square():
    random_size = random.randint(50, 300)  # 随机生成盒子边长
    return random_size, random_size


def find_positions_for_new_boxes(existing_boxes, container_width=1080, container_height=1920):
    def check_overlap(box1, box2):
        x1, y1 = box1['position']
        w1, h1 = box1['dimension']
        x2, y2 = box2['position']
        w2, h2 = box2['dimension']

        return not (x1 + w1 <= x2 or x2 + w2 <= x1 or y1 + h1 <= y2 or y2 + h2 <= y1)

    def is_valid_position(new_box):
        return False if new_box['position'][0] + new_box['dimension'][0] > container_width or new_box['position'][1] + \
                        new_box['dimension'][1] > container_height else all(
            not check_overlap(box, new_box) for box in existing_boxes)

    valid_boxes = []
    for box in existing_boxes:
        x, y = box['position']
        w, h = box['dimension']

        # Check the position to the right of the current box
        right_position = {'dimension': generate_random_square(), 'position': (x + w, y)}
        if right_position['dimension'][0] > 70 and is_valid_position(right_position):
            valid_boxes.append(right_position)

        # Check the position below the current box
        below_position = {'dimension': generate_random_square(), 'position': (x, y + h)}
        if below_position['dimension'][1] > 70 and is_valid_position(below_position):
            valid_boxes.append(below_position)

        # Check the position diagonally below-right of the current box
        diagonal_position = {'dimension': generate_random_square(), 'position': (x + w, y + h)}
        if diagonal_position['dimension'][0] > 70 and diagonal_position['dimension'][1] > 70 and is_valid_position(
                diagonal_position):
            valid_boxes.append(diagonal_position)

    return valid_boxes


if __name__ == '__main__':
    # Existing boxes data
    existing_boxes = [
        {'dimension': (291, 291), 'position': (0, 30)},
        {'dimension': (205, 205), 'position': (291, 30)},
        {'dimension': (199, 199), 'position': (496, 30)},
        {'dimension': (253, 253), 'position': (695, 30)},
        {'dimension': (291, 291), 'position': (0, 321)},
        {'dimension': (269, 269), 'position': (291, 321)},
        {'dimension': (274, 274), 'position': (560, 321)},
        {'dimension': (308, 308), 'position': (0, 612)},
        {'dimension': (201, 201), 'position': (308, 612)},
        {'dimension': (237, 237), 'position': (509, 612)},
        {'dimension': (261, 261), 'position': (746, 612)},
        {'dimension': (263, 263), 'position': (0, 920)},
        {'dimension': (274, 274), 'position': (263, 920)},
        {'dimension': (318, 318), 'position': (537, 920)},
        {'dimension': (200, 200), 'position': (855, 920)},
        {'dimension': (242, 242), 'position': (0, 1238)},
        {'dimension': (324, 324), 'position': (242, 1238)},
        {'dimension': (308, 308), 'position': (566, 1238)},
        {'dimension': (302, 302), 'position': (0, 1562)},
        {'dimension': (308, 308), 'position': (302, 1562)},
        {'dimension': (50, 50), 'position': (0, 1870)}
    ]
    # Find all possible positions for the new boxes

    possible_boxes = find_positions_for_new_boxes(existing_boxes, 1)
